/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

/**
 *
 * @author Ira
 */
public abstract class User extends Person {
	
	protected static GregorianCalendar now = new GregorianCalendar();
	protected static final int maxBorrow = 5;
	private List<Document> listDocs = new ArrayList<Document>(User.maxBorrow);
	private List<Document> listDocsReserve = new ArrayList<Document>(User.maxBorrow);
	
	public User(String name, String firstName, String pseudo, String pass, String type) {
		super(name, firstName, pseudo, pass, type);
	}
	
	public abstract int getBorrowTime();
	
	public boolean canBorrow() {
		if(this.listDocs.size() + this.listDocsReserve.size() < User.maxBorrow) {
			for(Document doc: this.listDocs) {
				// Si un document en retard, prets interdits
				if(doc.getDateAvaillable().before(User.now)) {
					return false;
				}
			}
			return true;
		}
		return false;
	}

	public boolean borrow(Document d) {
		if(this.canBorrow() && d.isBorrowable(this)) { 
			GregorianCalendar cal = new GregorianCalendar();
			cal.set(GregorianCalendar.HOUR, 0);
			cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
			cal.set(GregorianCalendar.MINUTE, 0);
			cal.set(GregorianCalendar.SECOND, 0);
			cal.set(GregorianCalendar.MILLISECOND, 0);
			d.setDateBorrow((GregorianCalendar)cal.clone());
			d.setBorrowBy(this);
			d.getListReserve().poll();
			if(d.getListReserve().isEmpty()) {
				cal.add(GregorianCalendar.DATE, this.getBorrowTime());
				d.setDateAvaillable((GregorianCalendar)cal.clone());
				d.setDate1stReserve((GregorianCalendar)cal.clone());
			}
			this.listDocsReserve.remove(d);
			this.listDocs.add(d);
			return true;
		}
		return false;
	}
	
	public boolean reserve(Document d) {
		if(this.canBorrow() && !this.equals(d.getBorrowBy()) && !d.getListReserve().contains(this)) {
			d.setDateAvaillable(new GregorianCalendar(1, 0, 1));
			d.getListReserve().add(this);
			this.listDocsReserve.add(d);
			return true;
		}
		return false;
	}

	public boolean stopReserve(Document d) {
		if(d.getListReserve().remove(this) && this.listDocsReserve.remove(d)) {
			if(d.getListReserve().isEmpty()) {
				// Calcul de la date de dispo
				GregorianCalendar cal = (GregorianCalendar) d.getDateBorrow().clone();
				User user = d.getBorrowBy();
				cal.roll(GregorianCalendar.DATE, user.getBorrowTime());
				d.setDateAvaillable(cal);
			}
			else {
				
			}
			return true;
		}
		return false;
	}
	
	public boolean bringBack(Document d) {
		if(this.listDocs.remove(d)) {
			d.setDateBorrow(null);
			d.setBorrowBy(null);
			//this.listDocsReserve.remove(d);
			if(d.getListReserve().isEmpty()) {
				d.setDateAvaillable(null);
				d.setDate1stReserve(null);
			}
			return true;
		}
		return false;
	}

	public boolean equals(User user) {
		if(user == null) {
			return false;
		}
		return this.id == user.id;
	}
	
	/**
	 * @return the listDocs
	 */
	public List<Document> getListDocs() {
		return listDocs;
	}

	/**
	 * @return the listDocsReserve
	 */
	public List<Document> getListDocsReserve() {
		return listDocsReserve;
	}
	
	public int getNBBorrow() {
		return this.listDocs.size();
	}
	
	public int getNBReserve() {
		return this.listDocsReserve.size();
	}
	
	public int getNBLate() {
		int late = 0;
		
		for(Document doc: this.listDocs) {
			if(doc.getDate1stReserve().before(User.now)) {
				++late;
			}
		}
		
		return late;
	}
	
	public void replaceDocument(Document before, Document after) {
		
	}
}
